package com.sys.app.entity.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sys.app.entity.token.BaseUserInfo;
import com.sys.app.entity.util.DateTimeUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 自定义填充公用字段值
 */
@Component
public class MyBatsDateConfig implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 起始版本 3.3.0(推荐使用)
        String time= DateTimeUtil.getNowTime();
        this.strictInsertFill(metaObject, "creator", Long.class, BaseUserInfo.getUser().getId());
        this.strictInsertFill(metaObject, "createTime", String.class, time);
        this.strictInsertFill(metaObject, "lastOperator", Long.class, BaseUserInfo.getUser().getId());
        this.strictInsertFill(metaObject, "lastOperateTime", String.class, time);
        this.strictInsertFill(metaObject, "ifDelete", Integer.class, 0);
        this.strictInsertFill(metaObject, "version", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 起始版本 3.3.0(推荐)
        this.strictInsertFill(metaObject, "lastOperator", Long.class, BaseUserInfo.getUser().getId());
        this.strictUpdateFill(metaObject, "lastOperateTime", String.class, DateTimeUtil.getNowTime());
    }
}
